package com.jubaozan.service.platform.decorator;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jubaozan.c3.framework.utils.ObjectUtil;
import com.jubaozan.c3.framework.utils.ResultHolder;
import com.jubaozan.dao.youme.domain.StoreUser;
import com.jubaozan.dao.youme.domain.StoreUserAudit;
import com.jubaozan.dao.youme.service.StoreUserAuditService;
import com.jubaozan.dao.youme.service.StoreUserService;
import com.jubaozan.feign.level.LevelFeignService;
import com.jubaozan.feign.level.vo.LevelVO;
import com.jubaozan.feign.user.UserFeignService;
import com.jubaozan.feign.user.vo.UserVO;
import com.jubaozan.service.platform.constants.PlatformStatusEnum;
import com.jubaozan.service.platform.exception.PlatformException;
import com.jubaozan.service.platform.vo.StoreUserAuditVO;
import com.jubaozan.service.platform.vo.TeamUserProgressVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName TeamAuditDecorator
 * @Description 团长入驻审核Decorator
 * @Author SunWei
 * @Version 1.0
 **/
@Slf4j
@Component
public class TeamAuditDecorator {

    private final StoreUserAuditService storeUserAuditService;

    private final UserFeignService userFeignService;

    private final StoreUserService storeUserService;

    private final LevelFeignService  levelFeignService;

    public TeamAuditDecorator(StoreUserAuditService storeUserAuditService, UserFeignService userFeignService, StoreUserService storeUserService, LevelFeignService levelFeignService) {
        this.storeUserAuditService = storeUserAuditService;
        this.userFeignService = userFeignService;
        this.storeUserService = storeUserService;
        this.levelFeignService = levelFeignService;
    }

    /**
     * 查询团长入驻审核信息列表
     * @param storeId
     * @param status
     * @param levelId
     * @param pageNo
     * @param pageSize
     * @return
     */
    public List<StoreUserAuditVO> queryTeamList(Long storeId,List<Integer> status,Integer levelId,Integer pageNo,Integer pageSize){

        Page<StoreUserAudit> page = new Page<>(pageNo, pageSize);

        QueryWrapper<StoreUserAudit> queryWrapper = new QueryWrapper<>();


        //审核状态
        if (!CollectionUtils.isEmpty(status)) {
            queryWrapper.in("status",status);
        }
        //门店id
        if (ObjectUtil.isNotEmpty(storeId)) {
            queryWrapper.eq("store_id", storeId);
        }
        //等级
        if (ObjectUtil.isNotEmpty(levelId)){
            queryWrapper.eq("target_level_id", levelId);
        }
        //创建一个集合对象
        List<StoreUserAuditVO> objects = new ArrayList<>();

        IPage<StoreUserAudit> page1 = storeUserAuditService.page(page, queryWrapper);
        List<StoreUserAudit> userAudits = page1.getRecords();
        if(CollectionUtils.isEmpty(userAudits)){
            return objects;
        }
        //获取集合中用户id
        List<Long> userIdList = userAudits.stream().map(p -> p.getUserId()).collect(Collectors.toList());
        //获取用户信息集合
        List<UserVO> userVOList = userFeignService.getUserByIds(userIdList).getBody();
        if(CollectionUtils.isEmpty(userVOList)){
            throw new PlatformException("获取用户信息失败");
        }

        for (StoreUserAudit userAudit : userAudits) {
            for(UserVO user : userVOList){
                if(userAudit.getUserId().equals(user.getId())){
                    StoreUserAuditVO auditVO = new StoreUserAuditVO();
                    auditVO.setName(user.getName());
                    auditVO.setAvatar(user.getAvatar());
                    auditVO.setStatus(userAudit.getStatus());
                    auditVO.setCreateAt(userAudit.getCreateAt());
                    auditVO.setSourceLevel(userAudit.getSourceLevel());
                    auditVO.setSourceLevelName(userAudit.getSourceLevelName());
                    auditVO.setTargetLevel(userAudit.getTargetLevel());
                    auditVO.setTargetLevelName(userAudit.getTargetLevelName());
                    auditVO.setStoreId(userAudit.getStoreId());
                    auditVO.setUserId(userAudit.getUserId());
                    auditVO.setRemarks(userAudit.getRemarks());
                    auditVO.setMobile(user.getMobile());
                    auditVO.setUpdateAt(userAudit.getUpdateAt());
                    auditVO.setRejectReason(userAudit.getRejectReason());
                    objects.add(auditVO);
                }
            }
        }
        return objects;
    }

    /**
     * 查询团长审核进度
     * @param storeId
     * @param userId
     * @return
     */
    public TeamUserProgressVO queryProgress(Long storeId,Long userId){
        TeamUserProgressVO progressVO = new TeamUserProgressVO();
        //查询推荐人id
        StoreUser storeUser = storeUserService.getOne(new QueryWrapper<StoreUser>().eq("store_id", storeId).eq("user_id", userId));
        //通过推荐人id查询推荐人信息
        UserVO user = userFeignService.getUserById(storeUser.getRefereeId()).getBody();
        progressVO.setRefereeAvatar(user.getAvatar());
        progressVO.setRefereeMobile(user.getMobile());
        progressVO.setRefereeName(user.getName());
        //查询用户信息
        UserVO userVO = userFeignService.getUserById(userId).getBody();
        progressVO.setName(userVO.getName());
        progressVO.setMobile(userVO.getMobile());
        //查询审核进度
        StoreUserAudit userAudit = storeUserAuditService.getOne(new QueryWrapper<StoreUserAudit>().eq("store_id", storeId).eq("user_id", userId));
        progressVO.setCreateAt(userAudit.getCreateAt());
        progressVO.setStatus(userAudit.getStatus());
        return progressVO;
    }

    /**
     * 团长审核通过
     * @param storeId
     * @param userId
     * @return
     */
    public Boolean pass(Long storeId,Long userId,Integer targetLevel){
        StoreUserAudit userAudit = storeUserAuditService.getOne(new QueryWrapper<StoreUserAudit>().eq("user_id", userId).eq("store_id", storeId));
        userAudit.setStatus(PlatformStatusEnum.PASS_AUDIT.status());
        userAudit.setUpdateAt(LocalDateTime.now());
        boolean result = storeUserAuditService.updateById(userAudit);
        if(result){
            StoreUser storeUser = storeUserService.getOne(new QueryWrapper<StoreUser>().eq("user_id", userId).eq("store_id", storeId));
            if(ObjectUtil.isEmpty(storeUser)){
                throw new PlatformException("门店信息为空");
            }
            LevelVO levelVO = levelFeignService.queryLevel(targetLevel, storeUser.getStoreId()).getBody();
            if(ObjectUtil.isEmpty(levelVO)){
                throw new PlatformException("等级信息为空");
            }
            storeUser.setLevelId(levelVO.getId());
            result = storeUserService.updateById(storeUser);
        }
        return result;
    }

    /**
     * 审核拒绝
     * @param storeId
     * @param userId
     * @param rejectReason
     * @return
     */
    public Boolean refuse(Long storeId,Long userId,String rejectReason){

        StoreUserAudit userAudit = storeUserAuditService.getOne(new QueryWrapper<StoreUserAudit>().eq("user_id", userId).eq("store_id", storeId));
        if(ObjectUtil.isEmpty(userAudit)){
            throw new PlatformException("门店用户信息审核为空");
        }
        userAudit.setRejectReason(rejectReason);
        userAudit.setStatus(PlatformStatusEnum.REFUSE_AUDIT.status());
        userAudit.setUpdateAt(LocalDateTime.now());
        return storeUserAuditService.saveOrUpdate(userAudit);
    }
}
